﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Utility;
using System.Collections.ObjectModel;

namespace Portal.Systems.Render
{
    public class PermissionManRenderBll
    {
        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                CheckPermission(SiteId);
                string ButtonRootUrl = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html = "";
                Html +=
                   " <script language=javascript>\r\n" +
                   "    function ClientReading()\r\n" +
                   "    {\r\n" +
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        AjaxOut = Portal.Systems.Render.PermissionManRenderBll.ServerSideReading(SiteId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n"+
                   "        {\r\n"+
                   "            alert(AjaxOut.InfoMessage);\r\n"+
                   "            return;\r\n"+
                   "        }\r\n"+
                   "        document.getElementById('divListModules').innerHTML=AjaxOut.HtmlContent;\r\n"+
                   "    }\r\n" +

                   "    function ClientExpandCollapse(Id)\r\n"+
                   "    {\r\n"+
                   "        ModuleId='divZoneContent'+Id;\r\n" +
                   "        if(document.getElementById(ModuleId).style.display=='none')\r\n"+
                   "        {\r\n"+
                   "            document.getElementById(ModuleId).style.display='block';\r\n"+
                   "        }\r\n"+
                   "        else\r\n"+
                   "        {\r\n" +
                   "            document.getElementById(ModuleId).style.display='none';\r\n" +
                   "        }\r\n" +
                   "    }\r\n"+
                   " </script>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "      <ul>\r\n" +
                   "          <li onclick=\"javascript:ClientReading();\"><img class=image src=\"" + ButtonRootUrl + "/Refresh.png\"><div>Đọc lại</div></li>\r\n" +
                   "          <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonRootUrl + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "      </ul>\r\n" +
                   " </div>\r\n" +
                   " <h3>Phân quyền truy cập cho các chức năng</h3>\r\n" +
                   " <div class=ListModule id=divListModules>" + ServerSideReading(SiteId).HtmlContent+ "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
        
        
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                IPermissionModule[] PermissionModules = WebPermissionModuleUtility.LoadPlugIn(SiteId);
           
                for (int iIndex = 0; iIndex < PermissionModules.Length; iIndex++)
                {
                    Collection<PermissionItemListCls> ColPermissionItemLists = PermissionModules[iIndex].GetPermissionItemLists(SiteId);
                    Html += "<div class=CssBlock>\r\n" +
                           "  <div class=ModuleTitle>" + PermissionModules[iIndex].ServiceName + "<input onclick=\"javascript:ClientExpandCollapse('" + PermissionModules[iIndex].ServiceId + "');\" type=button class=button value=\"Đóng / mở\" style=\"float:right;marin-right:2px;\"></div>\r\n" +
                           "  <div style=\"display:none;\" id=\"divZoneContent" + PermissionModules[iIndex].ServiceId + "\">\r\n" +
                           "  <table>\r\n";
                    for (int iIndexModule = 0; iIndexModule < ColPermissionItemLists.Count; iIndexModule++)
                    {
                        Html += "   <tr>\r\n" +
                                 "       <td class=tdPermission><input type=button class=button value=\"Phân quyền\" onclick=\"javascript:window.open('Permission.aspx?ptc=" + ColPermissionItemLists[iIndexModule].ptc + "&id=" + ColPermissionItemLists[iIndexModule].id + "&backAspx=PermissionMan.aspx','_self')\"></td>\r\n" +
                                 "       <td class=tdPermissionSubject>" + ColPermissionItemLists[iIndexModule].subject + "</td>\r\n" +
                                 "   </tr>\r\n";
                    }
                    Html += "</table>\r\n"+
                            "</div>\r\n"+
                           "</div>\r\n";
                }
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex) 
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
